package com.aifeng.hassan_food.mine

import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet_card.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class WalletCardActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet_card)
        initTitle(title = "添加银行卡", divider = false)
    }

    override fun getViewModel() = getViewModel<WalletViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        card_name.text = getString("realName")
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_sure -> {
                when {
                    card_num.rawText.isNullOrEmpty() -> {
                        card_num.requestFocus()
                        toast("请输入本人的银行卡卡号")
                        return
                    }
                    !BankcardHelper.checkBankCard(card_num.rawText) -> {
                        card_num.requestFocus()
                        toast("请输入正确的银行卡卡号")
                        return
                    }
                }

                getViewModel().bank(
                    "userid" to getString("userId"),
                    "bankUsername" to card_name.textString(),
                    "bankCardNumber" to (card_num.rawText ?: "")
                ).bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("银行卡添加成功！")
                            putShared("bankCard", card_num.rawText ?: "")
                            ActivityStack.screenManager.popActivities(this::class.java)
                        }
                    )
            }
        }
    }

}
